﻿using System;
using System.Windows.Forms;
using BLO;
using DTO;

namespace QuanLyBanHang
{
    public partial class frmKhachHang : Form
    {
        private readonly KhachHang_Blo _khachHangBlo = new KhachHang_Blo();

        public frmKhachHang()
        {
            InitializeComponent();
        }

        private void frmKhachHang_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void setEnableInput(bool b){
            txtDiaChi.Enabled = b;
        }

        private void LoadData()
        {
            dgvKhachHang.DataSource = _khachHangBlo.GetAllObject();
            dgvKhachHang.Columns["MaKH"].HeaderText = "Mã khách hàng";
            dgvKhachHang.Columns["DiaChi"].HeaderText = "Địa chỉ";
        }


        private void btnThem_Click(object sender, EventArgs e)
        {
            setEnableInput(true);
            btnSua.Enabled = false;
            btnXoa.Enabled = false;
            btnLuu.Enabled = true;
            btnBoQua.Enabled = true;
            btnThem.Enabled = false;
            txtMaKhach.Text = "";
            txtDiaChi.Text = "";
        }

        private void dgvKhachHang_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvKhachHang.Rows[e.RowIndex].Cells[0].Value == null || txtDiaChi.Enabled.Equals(true))
            {
                return;
            }
            txtMaKhach.Text = dgvKhachHang.Rows[e.RowIndex].Cells["MaKH"].Value.ToString();
            txtDiaChi.Text = dgvKhachHang.Rows[e.RowIndex].Cells["DiaChi"].Value.ToString();
            btnXoa.Enabled = true;
            btnSua.Enabled = true;
        }

        private void btnBoQua_Click(object sender, EventArgs e)
        {
            btnBoQua.Enabled = false;
            btnThem.Enabled = true;
            btnLuu.Enabled = false;
            btnSua.Enabled = false;
            LoadData();
            setEnableInput(false);
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtDiaChi.Text) || string.IsNullOrEmpty(txtMaKhach.Text))
            {
                MessageBox.Show("Bạn phải nhập đầy đủ thông tin!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            int result = 0;
            string msg = "";

            if (string.IsNullOrEmpty(txtMaKhach.Text))
            {
                result = _khachHangBlo.AddObject(new KhachHang_Dto
                                                     {
                                                         MaKH =
                                                             "KH" +
                                                             _khachHangBlo.AutoIncrementIDObject(),
                                                         DiaChi = txtDiaChi.Text.Trim()
                                                     });
                msg = result > 0 ? "Thêm khách hàng thành công!" : "Thêm khách hàng không thành công!";
            }
            else
            {
                result = _khachHangBlo.EditObject(txtMaKhach.Text, new KhachHang_Dto {DiaChi = txtDiaChi.Text.Trim()});
                msg = result > 0 ? "Cập nhập khách hàng thành công!" : "Cập nhập khách hàng không thành công!";
            }
            MessageBox.Show(msg, "Thông báo", MessageBoxButtons.OK,
                            result > 0 ? MessageBoxIcon.Information : MessageBoxIcon.Error);
            if (result < 0) return;

            btnLuu.Enabled = false;
            btnBoQua.Enabled = false;
            btnThem.Enabled = true;
            btnSua.Enabled = false;
            txtDiaChi.Enabled = false;
            LoadData();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            setEnableInput(true);
            btnXoa.Enabled = false;
            btnThem.Enabled = false;
            btnLuu.Enabled = true;
            btnBoQua.Enabled = true;
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("Bạn thực sự muốn xóa khách hàng này?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            
            if (dr == DialogResult.No) return;

            int result = result = _khachHangBlo.DeleteObject(txtMaKhach.Text);
            string msg = result > 0 ? "Xóa khách hàng thành công!" : "Xóa khách hàng không thành công!";
            MessageBox.Show(msg, "Thông báo", MessageBoxButtons.OK, result > 0 ? MessageBoxIcon.Information:  MessageBoxIcon.Error);

            if (result < 0)
                return;

            btnXoa.Enabled = false;
            btnSua.Enabled = false;
            LoadData();
        }
    }
}